package com.jianan.mis.app.basedata;

import static com.jianan.mis.util.ui.table.TableCreator.createTableColumn;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.jianan.mis.data.domain.View;
import com.jianan.mis.service.ViewService;
import com.jianan.mis.sys.IMessageKeys;
import com.jianan.mis.sys.SpringBeanFactory;

public class ViewView extends ViewPart {
	
	private Text txtViewId;
	private Text txtName;
	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			View view = (View) element;
			String text = table.getColumn(columnIndex).getText();
			if (text.equals(VIEW_NAME)) {
				return view.getName();
			} else if (text.equals(VIEW_VIEW_ID)) {
				return view.getViewId();
			} else {
				return null;
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	}
	
	class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			return ((List) inputElement).toArray();
		}
		public void dispose() {
		}
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	public static final String VIEW_NAME = "视图名称";

	public static final String VIEW_VIEW_ID = "视图系统id";

	public static String[] COLUMNS = null;
	
	private TableViewer viewTableViewer;
	
	private Table table;
	
	public static final String ID = "com.jianan.mis.app.basedata.ViewView"; //$NON-NLS-1$
	
	private ViewService viewService;

	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		
		if(!initializeServices()) {
			MessageDialog.openError(getViewSite().getShell(),
					IMessageKeys.OPEN_VIEW_FAIL, IMessageKeys.SERVICE_UNAVAILABLE);
			return;
		}
		
		Composite container = new Composite(parent, SWT.NONE);

		final Label label = new Label(container, SWT.NONE);
		label.setText("视图列表:");
		label.setBounds(20, 20, 73, 12);

		viewTableViewer = new TableViewer(container, SWT.FULL_SELECTION | SWT.BORDER);
		viewTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(final SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				if (selection.getFirstElement() != null) {
					View view = (View) selection.getFirstElement();
					txtName.setText(view.getName());
					txtViewId.setText(view.getViewId());
				}
			}
		});
		table = viewTableViewer.getTable();
		table.setBounds(39, 51, 562, 167);

		// Initialze table columns
		initializeTableViewer();

		final Button btnSave = new Button(container, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				View view = new View();
				view.setName(txtName.getText());
				view.setViewId(txtViewId.getText());
				try {
					viewService.save(view);
				} catch (Exception ex) {
					MessageDialog.openError(e.display.getActiveShell(),
							IMessageKeys.FAIL, ex.getMessage());
					return;
				}
				reset();
				getViewSite().getActionBars().getStatusLineManager()
						.setMessage("视图信息已保存.");
			}
		});
		btnSave.setText("添加");
		btnSave.setBounds(170, 315, 60, 28);

		final Button btnUpdate = new Button(container, SWT.NONE);
		btnUpdate.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) viewTableViewer.getSelection();
				if (selection.getFirstElement() != null) {
					View view = (View) selection.getFirstElement();
					view.setName(txtName.getText());
					view.setViewId(txtViewId.getText());
					try {
						viewService.update(view);
					} catch (Exception ex) {
						MessageDialog.openError(e.display.getActiveShell(),
								IMessageKeys.FAIL, ex.getMessage());
						return;
					}
					reset();
					getViewSite().getActionBars().getStatusLineManager()
							.setMessage("视图信息已更新.");
				} else {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请选择一个视图!");
				}
			}
		});
		btnUpdate.setText("更新");
		btnUpdate.setBounds(282, 316, 54, 28);

		final Button btnRemove = new Button(container, SWT.NONE);
		btnRemove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				IStructuredSelection selection = (IStructuredSelection) viewTableViewer.getSelection();
				if (selection.getFirstElement() != null) {
					if (MessageDialog.openConfirm(e.display.getActiveShell(),
							IMessageKeys.CONFIRM, "删除视图将会影响组别，确认删除吗?")) {
						View view = (View) selection.getFirstElement();
						try {
							viewService.remove(view);
						} catch (Exception ex) {
							MessageDialog.openError(e.display.getActiveShell(),
									IMessageKeys.FAIL, ex.getMessage());
							return;
						}
						reset();
						getViewSite().getActionBars().getStatusLineManager()
								.setMessage("视图信息已删除.");
					}
				} else {
					MessageDialog.openInformation(e.display.getActiveShell(),
							IMessageKeys.FAIL, "请选择一个视图!");
				}
			}
		});
		btnRemove.setText("删除");
		btnRemove.setBounds(385, 316, 60, 28);

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText("视图名称:");
		label_1.setBounds(40, 232, 60, 12);

		final Label label_2 = new Label(container, SWT.NONE);
		label_2.setText("视图系统id:");
		label_2.setBounds(40, 270, 73, 12);

		txtName = new Text(container, SWT.BORDER);
		txtName.setBounds(127, 229, 115, 25);

		txtViewId = new Text(container, SWT.BORDER);
		txtViewId.setBounds(127, 267, 281, 25);
		
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	private void initializeTableViewer() {

		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		createTableColumn(viewTableViewer, SWT.LEFT, VIEW_NAME, "", 100, null);
		createTableColumn(viewTableViewer, SWT.LEFT, VIEW_VIEW_ID, "", 300, null);

		viewTableViewer.setLabelProvider(new TableLabelProvider());
		viewTableViewer.setContentProvider(new ContentProvider());
		
		viewTableViewer.setInput(viewService.getAll());
	}
	
	private void reset() {
		txtName.setText("");
		txtViewId.setText("");
		viewTableViewer.setInput(viewService.getAll());
	}
	
	private boolean initializeServices() {
		viewService = (ViewService) SpringBeanFactory.getBean("viewService");
		if(viewService == null) {
			return false;
		}
		return true;
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
	}

	@Override
	public void setFocus() {
		// Set the focus
	}

}
